﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;

namespace UI
{
    public partial class FCreateRecord : Form
    {
        private int iRecordID = -1;     
        public FCreateRecord()
        {
            InitializeComponent();
            iRecordID = -1;
            LoadData();
        }

        public FCreateRecord(int iID)
        {
            InitializeComponent();
            iRecordID = iID;
            LoadData();
        }

        private void BindCombobox()
        {
            //填充Account组合框
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("Name");
            DataColumn dc2 = new DataColumn("Value");
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            for (int i = 0; i < FMain.accMan.AccountList.Count; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = FMain.accMan.AccountList[i].Name;
                dr[1] = FMain.accMan.AccountList[i].ID;

                dt.Rows.Add(dr);
            }
            cbAccount.DisplayMember = "Name";
            cbAccount.ValueMember = "Value";
            cbAccount.DataSource = dt;
            //填充Type组合框，需要新建一个DataTable的对象，而且名称不能相同
            DataTable dt1 = new DataTable();
            DataColumn dc11 = new DataColumn("Name");
            DataColumn dc21 = new DataColumn("Value");
            dt1.Columns.Add(dc11);
            dt1.Columns.Add(dc21);
            for (int i = 0; i < FMain.typMan.TypeList.Count; i++)
            {
                DataRow dr = dt1.NewRow();
                dr[0] = FMain.typMan.TypeList[i].Name;
                dr[1] = FMain.typMan.TypeList[i].ID;

                dt1.Rows.Add(dr);
            }
            cbType.DisplayMember = "Name";
            cbType.ValueMember = "Value";
            cbType.DataSource = dt1;

        }

        private void LoadData()
        {
            BindCombobox();
            if (iRecordID != -1)
            {
                
                BLL.Record rec=new BLL.Record();
                BLL.Account acc = new BLL.Account();
                BLL.Type typ = new BLL.Type();
                rec= FMain.rcdMan.GetRecord(iRecordID);
                acc = FMain.accMan.GetAccount(rec.AccountID);
                typ = FMain.typMan.GetType(rec.TypeID);
                dtp1.Text = rec.Time.ToString();
                cbType.SelectedText = typ.Name;
                cbAccount.SelectedText = acc.Name;
                tbAmount.Text = rec.Amount.ToString();
                if (rec.Remark!=null)
                     rtbRemark.Text = rec.Remark.ToString();
            }
        }  

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //下面的情况是对于新建收支记录的
            if (iRecordID == -1)
            {
                //若rbOne被选中，则表明只有一项需要添加
                if (rbOne.Checked == true)
                {
                    BLL.Record rec = new BLL.Record();
                    int count = FMain.rcdMan.RecordList.Count;
                    if (count != 0)
                    {
                        rec.ID = FMain.rcdMan.RecordList[count - 1].ID + 1;
                    }
                    else
                    {
                        rec.ID = 1;     //Record的第一项ID为1
                    }
                    rec.Time = DateTime.Parse(dtp1.Text.ToString());
                    rec.TypeID = int.Parse(cbType.SelectedValue.ToString());
                    rec.AccountID = int.Parse(cbAccount.SelectedValue.ToString());
                    
                    rec.Amount = double.Parse(tbAmount.Text.ToString());
                    rec.Remark = rtbRemark.Text.ToString();
                    FMain.rcdMan.AddRecord(rec);         //考虑增加一条记录对record.xml和RecordList的影响
                    //下面是考虑增加Record记录对AccountList和Account.xml的影响
                    BLL.Type typ = new BLL.Type();
                    typ = FMain.typMan.GetType(rec.TypeID);
                    int iIndex = FMain.accMan.GetAccountIndex(rec.AccountID);  //获取该Record对应Account在AccountList中的索引值
                    if (typ.Isincome)
                    {
                        FMain.accMan.AccountList[iIndex].Balance += rec.Amount;   //若为收入，则添加该记录后该索引值对应Account的余额应该增加
                    }
                    else
                    {
                        FMain.accMan.AccountList[iIndex].Balance -= rec.Amount;   //若为支出，则添加该记录后该索引值对应Account的余额应该减少
                    }
                    FMain.accMan.SaveDataToFile();  //保存更改至account.xml文件
                    this.Close();
                }
                //若rbOne未被选中，且用户填入有效数据，则表明需要添加多项
                else if (cbFrequency.SelectedItem != null && tbTimes.Text != "")
                {
                    for (int i = 0; i < int.Parse(tbTimes.Text.ToString()); i++)
                    {
                        BLL.Record rec = new BLL.Record();
                        int count = FMain.rcdMan.RecordList.Count;
                        if (count != 0)
                        {
                            rec.ID = FMain.rcdMan.RecordList[count - 1].ID + 1;
                        }
                        else
                        {
                            rec.ID = 1;     //Record的第一项ID为1
                        }
                        rec.TypeID = int.Parse(cbType.SelectedValue.ToString());
                        rec.AccountID = int.Parse(cbAccount.SelectedValue.ToString());
                        rec.Amount = double.Parse(tbAmount.Text.ToString());
                        rec.Remark = rtbRemark.Text.ToString();
                        DateTime dt = new DateTime();
                        dt = DateTime.Parse(dtp1.Text.ToString());
                        if (cbFrequency.SelectedItem.ToString() == "日")
                        {
                            rec.Time = dt.AddDays(i);
                        }
                        if (cbFrequency.SelectedItem.ToString() == "周")
                        {
                            rec.Time = dt.AddDays(7 * i);
                        }
                        if (cbFrequency.SelectedItem.ToString() == "月")
                        {
                            rec.Time = dt.AddMonths(i);
                        }
                        FMain.rcdMan.RecordList.Add(rec);
                    }
                    FMain.rcdMan.SaveDataToFile();
                    //下面是考虑增加Record记录对AccountList和Account.xml的影响
                    BLL.Type typ = new BLL.Type();
                    typ = FMain.typMan.GetType(int.Parse(cbType.SelectedValue.ToString()));
                    int iIndex = FMain.accMan.GetAccountIndex(int.Parse(cbAccount.SelectedValue.ToString()));  //获取该Record对应Account在AccountList中的索引值
                    if (typ.Isincome)
                    {
                        FMain.accMan.AccountList[iIndex].Balance += (int.Parse(tbTimes.Text.ToString()))*(double.Parse(tbAmount.Text.ToString()));    //若为收入，则添加该记录后该索引值对应Account的余额应该增加
                    }
                    else
                    {
                        FMain.accMan.AccountList[iIndex].Balance -= (int.Parse(tbTimes.Text.ToString())) * (double.Parse(tbAmount.Text.ToString()));   //若为支出，则添加该记录后该索引值对应Account的余额应该减少
                    }
                    FMain.accMan.SaveDataToFile();  //保存更改至account.xml文件
                    this.Close();
                }
            }
            //下面的情况是对于修改收支记录的
            else
            {
                //首先用rcdMan.UpdateRecord()函数修改该项
                BLL.Record rec = new BLL.Record();
                int count = FMain.rcdMan.RecordList.Count;
                rec = FMain.rcdMan.GetRecord(iRecordID);
                double iExAmount = rec.Amount; //记录原始Record中的Amount，后续修改需要使用
                rec.ID = iRecordID;
                rec.Time = DateTime.Parse(dtp1.Text.ToString());
                rec.TypeID = int.Parse(cbType.SelectedValue.ToString());
                rec.AccountID = int.Parse(cbAccount.SelectedValue.ToString());
                rec.Amount = double.Parse(tbAmount.Text.ToString());
                rec.Remark = rtbRemark.Text.ToString();
                FMain.rcdMan.UpdateRecord(rec);
                if (rbOne.Checked)
                {
                    //考虑增加Record记录对AccountList和Account.xml的影响
                    BLL.Type typ = new BLL.Type();
                    typ = FMain.typMan.GetType(rec.TypeID);
                    int iIndex = FMain.accMan.GetAccountIndex(rec.AccountID);  //获取该Record对应Account在AccountList中的索引值
                    if (typ.Isincome)
                    {
                        FMain.accMan.AccountList[iIndex].Balance -= iExAmount;   //若为收入，则修改该记录后该索引值对应Account的余额应该先减去原Amount，再增加新Amount
                        FMain.accMan.AccountList[iIndex].Balance += rec.Amount;
                    }
                    else
                    {
                        FMain.accMan.AccountList[iIndex].Balance += iExAmount;   //若为支出，则修改该记录后该索引值对应Account的余额应该先增加原Amount，再减去新Amount
                        FMain.accMan.AccountList[iIndex].Balance -= rec.Amount;
                    }
                    FMain.accMan.SaveDataToFile();  //保存更改至account.xml文件
                    this.Close();
                }
                //若rbOne未被选中，且用户填入有效数据，则表明除了更新该项还需要添加多项
                else if (cbFrequency.SelectedItem != null && tbTimes.Text != "")
                {
                    for (int i = 0; i < (int.Parse(tbTimes.Text.ToString()))-1; i++)
                    {
                        BLL.Record rec1 = new BLL.Record();
                        count = FMain.rcdMan.RecordList.Count;
                        if (count != 0)
                        {
                            rec1.ID = FMain.rcdMan.RecordList[count - 1].ID + 1;
                        }
                        else
                        {
                            rec1.ID = 1;     //Record的第一项ID为1
                        }
                        rec1.TypeID = int.Parse(cbType.SelectedValue.ToString());
                        rec1.AccountID = int.Parse(cbAccount.SelectedValue.ToString());
                        rec1.Amount = double.Parse(tbAmount.Text.ToString());
                        rec1.Remark = rtbRemark.Text.ToString();
                        DateTime dt = new DateTime();
                        dt = DateTime.Parse(dtp1.Text.ToString());
                        if (cbFrequency.SelectedItem.ToString() == "日")
                        {
                            rec1.Time = dt.AddDays(i);
                        }
                        if (cbFrequency.SelectedItem.ToString() == "周")
                        {
                            rec1.Time = dt.AddDays(7 * i);
                        }
                        if (cbFrequency.SelectedItem.ToString() == "月")
                        {
                            rec1.Time = dt.AddMonths(i);
                        }
                        FMain.rcdMan.RecordList.Add(rec1);
                    }
                    FMain.rcdMan.SaveDataToFile();
                    //下面是考虑增加Record记录对AccountList和Account.xml的影响
                    BLL.Type typ = new BLL.Type();
                    typ = FMain.typMan.GetType(int.Parse(cbType.SelectedValue.ToString()));
                    int iIndex = FMain.accMan.GetAccountIndex(int.Parse(cbAccount.SelectedValue.ToString()));  //获取该Record对应Account在AccountList中的索引值
                    if (typ.Isincome)
                    {
                        FMain.accMan.AccountList[iIndex].Balance -= iExAmount;
                        FMain.accMan.AccountList[iIndex].Balance += (int.Parse(tbTimes.Text.ToString())) * (double.Parse(tbAmount.Text.ToString()));    //若为收入，则修改该记录后该索引值对应Account的余额应该先减去原Amount，再增加新Amount
                    }
                    else
                    {
                        FMain.accMan.AccountList[iIndex].Balance += iExAmount;
                        FMain.accMan.AccountList[iIndex].Balance -= (int.Parse(tbTimes.Text.ToString())) * (double.Parse(tbAmount.Text.ToString()));   //若为支出，则修改该记录后该索引值对应Account的余额应该先增加原Amount，再减去新Amount
                    }
                    FMain.accMan.SaveDataToFile();  //保存更改至account.xml文件
                    this.Close();
                }
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rbOne_CheckedChanged(object sender, EventArgs e)
        {
            pnlFrequency.Visible = false;
        }

        private void rbMany_CheckedChanged(object sender, EventArgs e)
        {
            pnlFrequency.Visible = true;
        }

       


    }
}
